﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SMP.Resource
{
    using SMP.Domain;
    using System.Data;

    public class Phieuxuat_Res : BaseDB
    {
        Kho_Res _objKho = new Kho_Res();

        #region Phiếu nhập

        public DataTable GetAll_Phieuxuat()
        {
            return ExecuteQuery("EXEC GetAll_Phieuxuat ");
        }

        public DataTable GetAll_Hinhthuc_Xuathang()
        {
            return ExecuteQuery(@" SELECT Id_Hinhthuc_Xuathang, Ten_Hinhthuc_Xuathang FROM tb_hinhthuc_xuathang");
        }

        public DataTable Get_Phieuxuat_Check_Used_By_Id_Phieuxuat(long id_phieuxuat)
        {
            string sql = @"EXEC Get_Phieuxuat_Check_Used_By_Id_Phieuxuat {0}";
            sql = string.Format(sql, id_phieuxuat);
            return ExecuteQuery(sql);
        }

        public bool Delete_Phieuxuat(object id_Phieuxuat)
        {
            string sql = string.Format(@"EXEC Delete_Phieuxuat {0}", id_Phieuxuat);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Insert_Phieuxuat(Phieuxuat px)
        {
            string sql = string.Format("SET DATEFORMAT dmy; EXEC Insert_Phieuxuat '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', N'{7}' ",
                                        px.Id_Nha_Cungcap, px.Id_Hinhthuc_Xuathang, px.Ngayxuat, px.Tongtien, px.Check_No, px.Sotien_Thanhtoan, px.Sotien_Conlai, px.Ghichu);
            return ExecuteQuery(sql);
        }

        public bool Update_Phieuxuat(Phieuxuat px)
        {
            string sql = string.Format(@"SET DATEFORMAT dmy; EXEC Update_Phieuxuat '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', N'{7}', '{8}' ",
                                px.Id_Nha_Cungcap, px.Id_Hinhthuc_Xuathang, px.Ngayxuat, px.Tongtien, px.Check_No, px.Sotien_Thanhtoan, px.Sotien_Conlai, px.Ghichu, px.Id_Phieuxuat);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        #endregion

        #region Phiếu nhập chi tiết

        public DataTable GetAll_Mathang_By_Id_Phieuxuat(object id_Phieuxuat)
        {
            string sql = string.Format(@"GetAll_Mathang_By_Id_Phieuxuat {0} ", id_Phieuxuat);
            return ExecuteQuery(sql);
        }

        private object Insert_Phieuxuat_Chitiet(Phieuxuat phieuxuat)
        {
            string sql = string.Format("EXEC Insert_Phieuxuat_Chitiet '{0}', '{1}', {2}, '{3}', '{4}' ",
                                                                phieuxuat.Id_Phieuxuat, phieuxuat.Id_Mathang, phieuxuat.Soluong, phieuxuat.Dongia, phieuxuat.Thanhtien);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        private object Delete_Phieuxuat_Chitiet(object id_Phieuxuat)
        {
            string sql = string.Format(@"EXEC Delete_Phieuxuat_Chitiet {0}", id_Phieuxuat);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update_Phieuxuat_Chitiet_Collection(DataTable dtb, object id_Phieuxuat, string action)
        {
            try
            {
                if (action == "Insert")
                {
                    foreach (DataRow r in dtb.Rows)
                        Insert_Phieuxuat_Chitiet(Get_Phieuxuat_Chitiet(r, id_Phieuxuat));
                    _objKho.Update_Kho();
                }
                else
                {
                    Delete_Phieuxuat_Chitiet(id_Phieuxuat);
                    foreach (DataRow r in dtb.Rows)
                        Insert_Phieuxuat_Chitiet(Get_Phieuxuat_Chitiet(r, id_Phieuxuat));
                    _objKho.Update_Kho();
                }

            }
            catch (Exception ex)
            {
                throw ex;
                return false;
            }
            return true;
        }

        private Phieuxuat Get_Phieuxuat_Chitiet(DataRow row, object id_Phieuxuat)
        {
            Phieuxuat phieuxuat = new Phieuxuat();

            phieuxuat.Id_Phieuxuat = id_Phieuxuat;

            if (row["Id_Phieuxuat_Chitiet"] + "" != "")
                phieuxuat.Id_Phieuxuat_Chitiet = row["Id_Phieuxuat_Chitiet"];

            if (row["Id_Mathang"] + "" != "")
                phieuxuat.Id_Mathang = row["Id_Mathang"];

            if (row["Soluong"] + "" != "")
                phieuxuat.Soluong = row["Soluong"];

            if (row["Dongia"] + "" != "")
                phieuxuat.Dongia = row["Dongia"];

            if (row["Thanhtien"] + "" != "")
                phieuxuat.Thanhtien = row["Thanhtien"];

            return phieuxuat;
        }

        #endregion





    }
}
